<?php
class LanguageController extends AppAdminController {

    /*前台WEB网站语言*/
    public function actionIndex() {

        $this->activemenu = array(
            'level_1'=>'语言管理',
            'level_2'=>'语言列表'
        );

        $langs = $this->connection->createCommand("select * from xm_lang where mark !='rkcms' order by id asc")->queryAll();
        $this->render ('index',array(
            'langs'=>$langs
        ));
    }

    /*后台网站语言*/
    public function actionBack(){
        $langs = $this->connection->createCommand("select * from xm_lang where mark ='rkcms' order by id asc")->queryAll();
        $this->render ('back',array(
            'langs'=>$langs
        ));
    }

    /*语言显示编辑*/
    public function actionShowedit(){

        $this->activemenu = array(
            'level_1'=>'语言管理',
            'level_2'=>'语言列表'
        );

        $id = $_GET['id'];
        $lang = $this->connection->createCommand("select * from xm_lang where id = ".$id)->queryRow();
        $this->data['lang'] = $lang;
        $this->render("showedit",$this->data);
    }

    /*语言进行编辑*/
    public function actionEdit(){

        $id = $_POST['id'];
        $useok = $_POST['useok'];
        $isdefault = $_POST['isdefault'];
        $mark = $_POST['mark'];

        $this->connection->createCommand("update xm_lang set useok = {$useok},isdefault={$isdefault} where id = {$id}")->execute();
        $this->connection->createCommand("update xm_lang set isdefault=0 where id != {$id}")->execute();
        if($mark == 'xmcms'){
            $this->redirect("index.php?r=admin/language/back");
        }else{
            $this->message(true,0);
        }
    }

    /**显示语言编辑列表**/
    public function actionShoweditlang(){

        $this->activemenu = array(
            'level_1'=>'语言管理',
            'level_2'=>'语言列表'
        );

        $lang = $_GET['lang'];

        $langs = $this->connection->createCommand("select s.*,t.translation from xm_sourcemessage s 
left JOIN xm_translatedmessage t on s.id = t.id 
where t.`language` = '{$lang}' order by s.category asc")->queryAll();

        $this->data['langs'] = $langs;

        $lang = $this->connection->createCommand("select * from xm_lang where lang = '{$lang}'")->queryRow();
        $this->data['lang'] = $lang;

        $this->render("showeditlang",$this->data);
    }

    public function actionUpdatetransaction(){
        $id = $_GET['id'];
        $val = $_GET['val'];
        $lang = $_GET['lang'];

        $command = $this->connection->createCommand("update xm_translatedmessage set translation = :translation where language = :language and id = :id");
        $command->bindParam(":translation",$val,PDO::PARAM_STR);
        $command->bindParam(":language",$lang,PDO::PARAM_STR);
        $command->bindParam(":id",$id,PDO::PARAM_INT);
        $command->execute();

        $this->message(true,"翻译修改成功");

    }




}